package dm1101.demo02;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Ln
 * @version 终极版
 * @date 2022/11/1 16:13
 */
public class MenuLockTest {
    public static void main(String[] args) {
        MyTicket1 myTicket1 = new MyTicket1();


        Thread t1 = new Thread(myTicket1,"窗口A");
        Thread t2 = new Thread(myTicket1,"窗口B");
        Thread t3 = new Thread(myTicket1,"窗口C");
        Thread t4 = new Thread(myTicket1,"窗口D");

        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
class MyTicket1 implements Runnable{

    private static int ticket=1000;
    private static Object lock=new Object();

    @Override
    public void run() {
        Lock reentrantLock = new ReentrantLock();
        while (true){
            try {
                reentrantLock.lock();
                if (ticket>0){
                    System.out.println(Thread.currentThread().getName() + "卖了一张票，剩余:" + --ticket);
                } else {
                    System.out.println("票已售空，请明天再来！！");
                    break;
                }
            }
            finally {
                reentrantLock.unlock();
            }

        }
    }
}
